package com.open4cn.simulator.core.device;

import java.io.Serializable;
import java.util.StringJoiner;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description
 * @Email yinxiaopeng@zj.tech
 * @date 2022-03-14 23:44
 */
public class DeviceStatus<S> implements Serializable {

    // 设备id
    private String deviceId;

    // 设备品牌
    private String brand;

    // 设备型号
    private String model;

    // 运行中
    private StatusEnum status;

    // 状态自定义数据
    private S data;

    public DeviceStatus(String deviceId, String brand, String model) {
        this.status = StatusEnum.WAITING;
        this.deviceId = deviceId;
        this.brand = brand;
        this.model = model;
    }

    public DeviceStatus(String deviceId, String brand, String model, S data) {
        this.status = StatusEnum.WAITING;
        this.deviceId = deviceId;
        this.brand = brand;
        this.model = model;
        this.data = data;
    }

    public StatusEnum getStatus() {
        return status;
    }

    public boolean runable() {
        return StatusEnum.WAITING.equals(status);
    }

    public boolean beRun() {
        this.status = StatusEnum.RUNNING;
        return isRunning();
    }

    public void beConnecting() {
        this.status = StatusEnum.CONNECTING;
    }

    public boolean isRunning() {
        return StatusEnum.RUNNING.equals(status);
    }

    public boolean isSuspended() {
        return StatusEnum.SUSPENDED.equals(status);
    }

    public boolean beSuspended() {
        this.status = StatusEnum.SUSPENDED;
        return isSuspended();
    }


    public boolean isClosed() {
        return StatusEnum.CLOSED.equals(status);
    }

    public boolean beClose() {
        this.status = StatusEnum.CLOSED;
        return isClosed();
    }

    public String getDeviceId() {
        return deviceId;
    }

    public String getDeviceName() {
        StringJoiner joiner = new StringJoiner("-");
        joiner.add(brand);
        joiner.add(model);
        return joiner.toString();
    }

    public String getBrand() {
        return brand;
    }

    public String getModel() {
        return model;
    }

    public S getData() {
        return data;
    }

    public void setData(S data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "DeviceStatus{" +
                "deviceId='" + deviceId + '\'' +
                ", brand='" + brand + '\'' +
                ", model='" + model + '\'' +
                ", status=" + status +
                ", data=" + data +
                '}';
    }

    /**
     * --------------------------------------------------------------
     **/
    public enum StatusEnum {
        WAITING,
        CONNECTING,
        RUNNING,
        SUSPENDED,
        CLOSED;
    }

}
